package com.kjtpay.design_pattern.adapter;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.kjtpay.design_pattern.R;
import com.kjtpay.design_pattern.builder.BuilderActivity;

public class AdapterActivity extends AppCompatActivity {

    public static void launch(Context context) {
        if (null != context) {
            Intent intent = new Intent();
            intent.setClass(context, AdapterActivity.class);
            if (!(context instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        }
    }

    CheckBox isForeignCheckBox;
    EditText forwardEditText;
    EditText centerEditText;
    EditText guardEditText;
    TextView actionTextView;

    Player forward;
    Player guard;
    Player center;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adapter);
        setTitle("适配器模式");

        isForeignCheckBox = findViewById(R.id.checkBoxIsForeign);
        forwardEditText = findViewById(R.id.editTextForward);
        centerEditText = findViewById(R.id.editTextCenter);
        guardEditText = findViewById(R.id.editTextGuard);
        actionTextView = findViewById(R.id.textViewAction);
    }

    public void onAttackClick(View view) {
        buildTeam();
        forward.attack();
        center.attack();
        guard.attack();
        String action = forward.show() + "\n"
                + center.show() + "\n"
                + guard.show();
        actionTextView.setText(action);
    }

    public void onDefenceClick(View view) {
        buildTeam();
        forward.defence();
        center.defence();
        guard.defence();
        String action = forward.show() + "\n"
                + center.show() + "\n"
                + guard.show();
        actionTextView.setText(action);
    }

    private void buildTeam() {
        forward = new Forward(forwardEditText.getText().toString());
        guard = new Guard(guardEditText.getText().toString());
        if (isForeignCheckBox.isChecked()) {
            center = new Translator(centerEditText.getText().toString());
        } else {
            center = new Center(centerEditText.getText().toString());
        }
    }
}